<?php

namespace App\Models;

use App\User;
use App\Util\Tool;
use App\Util\Url;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Mrgoon\AliSms\AliSms;

class FinancialStatementsLog extends Model
{

    protected $table = "c_financial_statements_logs";

    /**
     * @param $param
     *总账单
     */
    public function getAllTotal($param)
    {
        $where = [];
        $where[] = ['work_id', '=', $param['work_id']];
        $where[] = ['add_or_sub', '=', 1];
        $w[] = ['work_id', '=', $param['work_id']];
        $w[] = ['add_or_sub', '=', 2];
        if (isset($param['type']) && $param['type'] == 1) { //今日
            $weekStart = Carbon::now()->startOfDay();
            $weekEnd = Carbon::now()->endOfDay();
            $res = $this->setDataMoney($where, $w, $weekStart, $weekEnd);
        } elseif (isset($param['type']) && $param['type'] == 2) { //昨日
            $weekStart = Carbon::now()->subDay()->startOfDay();
            $weekEnd = Carbon::now()->subDay()->endOfDay();
            $res = $this->setDataMoney($where, $w, $weekStart, $weekEnd);
        } elseif (isset($param['type']) && $param['type'] == 3) {  //本周
            $weekStart = Carbon::now()->startOfWeek();
            $weekEnd = Carbon::now()->endOfWeek();
            $res = $this->setDataMoney($where, $w, $weekStart, $weekEnd);
        } elseif (isset($param['type']) && $param['type'] == 4) {  //上周
            $weekStart = Carbon::now()->subWeek()->startOfWeek();
            $weekEnd = Carbon::now()->subWeek()->endOfWeek();
            $res = $this->setDataMoney($where, $w, $weekStart, $weekEnd);
        } elseif (isset($param['type']) && $param['type'] == 5) {   //本月
            $weekStart = Carbon::now()->startOfMonth();
            $weekEnd = Carbon::now()->endOfMonth();
            $res = $this->setDataMoney($where, $w, $weekStart, $weekEnd);
        } else {
            $weekStart = "2024-01-01 00: 00: 00";
            $weekEnd = date("Y-m-d H:i:s", time());;
            $res = $this->setDataMoney($where, $w, $weekStart, $weekEnd);
        }
        //提现金额
        $withdrawal = (new WithdrawalLog())->where([['user_id', '=', $param['work_id']], ['type', '=', 1], ['status', '=', 1]])->sum("money");
//        查询工作室信息
        $info = (new DoctorWorks())->where([['id', '=', $param['work_id']]])->first();
        $res['day_no_settle_money'] = $info->wait_surplus;
        $res['day_settle_money'] =  number_format($info->surplus - $info->deposit_surplus,2); //余额去掉押金宝金额
        $res['deposit_surplus'] = $info->deposit_surplus; //押金宝余额
        $res['withdrawal'] = (string)$withdrawal;
        $res['day_deposit_money'] = self::getDepositingMoney($param,$weekStart,$weekEnd);
        return $res;
    }

    /**
     * 获取正在使用中的押金金额
     */
    public static function getDepositingMoney($filter,$start,$end)
    {
        $res = (new OrderGoods())
            ->from("c_order_goods as order_goods")
            ->join("c_orders as order",'order.id','=','order_goods.order_id')
            ->whereBetween('order.pay_time', [$start, $end])
            ->where([['order.order_type', '=', 5],['order.order_status','=',0],['order.work_id','=',$filter['work_id']]])->sum("order_price");
        return number_format($res,2);
    }

    /**
     * @param $where
     * @param $w
     * @param $weekStart
     * @param $weekEnd
     * @return mixed
     * 封装数据
     */
    public function setDataMoney($where, $w, $weekStart, $weekEnd)
    {
        //加钱数据
        $day_all_money_add = $this->whereBetween('date_str', [$weekStart, $weekEnd])->where($where)->where([['type', '!=', 5]])->sum("money");//所有
//        $day_settle_money_add = $this->whereBetween('date_str', [$weekStart, $weekEnd])->where($where)->where([['settle_status', '=', 1]])->sum("money");//已结算
//        $day_no_settle_money_add = $this->whereBetween('date_str', [$weekStart, $weekEnd])->where($where)->where([['settle_status', '=', 0]])->sum("money");//未结算
        $day_consult_money_add = $this->whereBetween('date_str', [$weekStart, $weekEnd])->where($where)->where([['type', '=', 3]])->sum("money");//咨询
        $day_course_money_add = $this->whereBetween('date_str', [$weekStart, $weekEnd])->where($where)->where([['type', '=', 1]])->sum("money");//课程
        $day_goods_money_add = $this->whereBetween('date_str', [$weekStart, $weekEnd])->where($where)->where([['type', '=', 2]])->sum("money");//商品
        $day_shang_money_add = $this->whereBetween('date_str', [$weekStart, $weekEnd])->where($where)->where(['type'=> 4])->sum("money");//打赏
//        $day_deposit_money_add = $this->whereBetween('date_str', [$weekStart, $weekEnd])->where($where)->where([['type', '=', 5]])->sum("money");//押金
        //减钱数据
        $day_all_money_sub = $this->whereBetween('date_str', [$weekStart, $weekEnd])->where($w)->where([['type', '!=', 5]])->sum("money");//所有
//        $day_settle_money_sub = $this->whereBetween('date_str', [$weekStart, $weekEnd])->where($w)->where([['settle_status', '=', 0]])->sum("money");//已结算
//        $day_no_settle_money_sub = $this->whereBetween('date_str', [$weekStart, $weekEnd])->where($w)->sum("money");//已结算
        $day_consult_money_sub = $this->whereBetween('date_str', [$weekStart, $weekEnd])->where($w)->where([['type', '=', 3]])->sum("money");//咨询
        $day_course_money_sub = $this->whereBetween('date_str', [$weekStart, $weekEnd])->where($w)->where([['type', '=', 1]])->sum("money");//课程
        $day_goods_money_sub = $this->whereBetween('date_str', [$weekStart, $weekEnd])->where($w)->where([['type', '=', 2]])->sum("money");//商品
        $day_shang_money_sub = $this->whereBetween('date_str', [$weekStart, $weekEnd])->where($w)->where(['type'=>4])->sum("money");//打赏
//        $day_deposit_money_sub = $this->whereBetween('date_str', [$weekStart, $weekEnd])->where($w)->where([['type', '=', 5]])->sum("money");//押金
        $day_all_money = $day_all_money_add - $day_all_money_sub;
//        $day_settle_money = $day_settle_money_add - $day_settle_money_sub;
//        $day_no_settle_money = $day_no_settle_money_add - $day_no_settle_money_sub;
        $day_consult_money = $day_consult_money_add - $day_consult_money_sub;
        $day_course_money = $day_course_money_add - $day_course_money_sub;
        $day_goods_money = $day_goods_money_add - $day_goods_money_sub;
        $day_shang_money = $day_shang_money_add - $day_shang_money_sub;
//        $day_deposit_money = $day_deposit_money_add - $day_deposit_money_sub;
        $total['day_all_money'] = number_format($day_all_money, 2);
//        $total['day_settle_money'] = number_format($day_settle_money,2);
//        $total['day_no_settle_money'] = number_format($day_no_settle_money,2);
        $total['day_consult_money'] = number_format($day_consult_money, 2);
        $total['day_course_money'] = number_format($day_course_money, 2);
        $total['day_goods_money'] = number_format($day_goods_money, 2);
        $total['day_shang_money'] = number_format($day_shang_money, 2);
//        $total['day_deposit_money'] = number_format($day_deposit_money, 2);
        return $total;
    }


    /**
     * @param $param
     * @return mixed
     * 账单列表
     */
    public function getAllStatementList($param)
    {
        $where = [];
        $where[] = ['work_id', '=', $param['work_id']];
        if (isset($param['type'])) {
            $where[] = ['type', '=', $param['type']];
        }
        if (isset($param['settle_status'])) {
            $where[] = ['settle_status', '=', $param['settle_status']];
        }
        $result = $this->where($where)->orderBy("id", "desc")->simplePaginate()->toArray();
        $order = new Order();
        $order_goods = new OrderGoods();
        $host = new Url();
        foreach ($result['data'] as $k => &$v) {
            $order_info = $order->where([['id', '=', $v['order_id']]])->first();
            if (!empty($order_info)) {
                $goods_info = $order_goods->where([['order_id', '=', $order_info->id]])->first();
                $goods_info->goods_img = $host::getHttpHost() . 'upload/' . $goods_info->goods_img;
                $order_info->goods_info = $goods_info;
                if ($order_info->order_type == 3 || $order_info->order_type == 4) {
                    $order_info->patient_name = (new OrderCaseDescribe())->where([['order_id', '=', $order_info->id]])->value("name");
                }
            }
            $v['order_goods'] = $order_info;
        }
        return $result;
    }

    /***
     * @param $data
     * @param int $id
     * @return mixed
     * 操作账单记录
     */
    public static function setStatementsLog($data, $where = [])
    {
        if (!empty($where)) {
            $res = self::where($where)->update($data);
        } else {
            $res = self::insert($data);
        }
        return $res;
    }


    /**
     * @param $order
     * 商品====订单退款
     * 退回账单(只退账单,不退金额)
     * 将待结算金额减掉
     */
    public function setGoodsOrderBack($order_id)
    {
        $res = (new Order())->getOrderDetail($order_id);
        //退款成功
        $order_goods = $res->order_goods;
        $data['sn'] = $res->sn;
        $data['date_str'] = date("Y-m-d H:i:s", time());
        $data['goods_num'] = $res->goods_num;
        $data['names'] = $order_goods->goods_name;
        $data['work_id'] = $res->work_id;
        $data['goods_id'] = $order_goods->goods_id;
        $data['order_id'] = $res->id;
        $data['updated_at'] = date("Y-m-d H:i:s", time());
        $data['created_at'] = date("Y-m-d H:i:s", time());
        $data['type'] = 2;
        $data['settle_status'] = 1;
        $data['add_or_sub'] = 2;
        $goods = (new ProductGoods())->where([['id', '=', $order_goods->goods_id]])->first();
        $data['divide_type'] = 1;
        $wk = new DoctorWorks();
        if ($goods->product_source == 1) { //工作室产品
            if ($goods->supplier_work_id == $res->work_id) { //工作室自己的产品
                //工作室退款账单
                $info = $this
                    ->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', $res->work_id], ['divide_type', '=', 1], ['add_or_sub', '=', 2]])
                    ->first();
                if (empty($info)) {
                    $data['money'] = $res->self_work_price;
                    $data['remarks'] = "商品取消订单工作室退款";
                    if ($res->self_work_price > 0) {
                        $this->insert($data);
                    }
                    //将原来的商品账单改为已结算
                    $this->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', $res->work_id], ['divide_type', '=', 1], ['add_or_sub', '=', 1], ['settle_status', '=', 0]])
                        ->update(['settle_status' => 1, 'is_log' => 1]);
                    //将工作室待结算金额剪掉
                    $wk->where([['id', '=', $res->work_id]])->decrement("wait_surplus", $res->self_work_price);
                }
                //平台退回账单
                $info = $this
                    ->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', 0], ['divide_type', '=', 3], ['add_or_sub', '=', 2]])
                    ->first();
                if (empty($info)) {
                    $data['money'] = $res->platform_money;
                    $data['divide_type'] = 3;
                    $data['work_id'] = 0;
                    $data['remarks'] = "商品取消订单平台退款";
                    if ($res->platform_money > 0) {
                        $this->insert($data);
                    }
                    //将原来的商品账单改为已结算
                    $this->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', 0], ['divide_type', '=', 3], ['add_or_sub', '=', 1], ['settle_status', '=', 0]])
                        ->update(['settle_status' => 1, 'is_log' => 1]);
                }
            } else { //分享产品  暂时没有用
                //本工作室账单情况
                $info = $this
                    ->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', $res->work_id], ['divide_type', '=', 1], ['add_or_sub', '=', 2]])
                    ->first();
                if (empty($info)) {
                    $data['remarks'] = "分享商品取消订单退款";
                    $data['money'] = $res->self_work_price;
                    $this->insert($data);
                }
                //分享工作室账单情况
                $info = $this
                    ->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', $goods->supplier_work_id], ['divide_type', '=', 1], ['add_or_sub', '=', 2]])
                    ->first();
                if (empty($info)) {
                    $data['remarks'] = "分享商品取消订单退款";
                    $data['money'] = $res->share_work_price;
                    $data['work_id'] = $goods->supplier_work_id;
                    $this->insert($data);
                }
                //平台退回账单
                $info = $this
                    ->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', 0], ['divide_type', '=', 3], ['add_or_sub', '=', 2]])
                    ->first();
                if (empty($info)) {
                    $data['money'] = $res->platform_money;
                    $data['divide_type'] = 3;
                    $data['work_id'] = 0;
                    $data['remarks'] = "商品取消订单退款";
                    $this->insert($data);
                }
            }
        } elseif ($goods->product_source == 2) {  //供应商产品
            //本工作室账单情况
            $info = $this
                ->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', $res->work_id], ['divide_type', '=', 1], ['add_or_sub', '=', 2]])
                ->first();
            if (empty($info)) {
                $data['money'] = $res->self_work_price;
                $data['remarks'] = "供应商商品取消订单退款";
                if ($res->self_work_price > 0) {
                    $this->insert($data);
                }
                //将原来的商品账单改为已结算
                $this->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', $res->work_id], ['divide_type', '=', 1], ['add_or_sub', '=', 1], ['settle_status', '=', 0]])
                    ->update(['settle_status' => 1, 'is_log' => 1]);
                //将工作室待结算金额剪掉
                $wk->where([['id', '=', $res->work_id]])->decrement("wait_surplus", $res->self_work_price);
            }
            //供应商账单情况
            $info = $this
                ->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', $goods->supplier_work_id], ['divide_type', '=', 2], ['add_or_sub', '=', 2]])
                ->first();
            if (empty($info)) {
                $data['money'] = $res->supplier_price;
                $data['work_id'] = $goods->supplier_work_id;
                $data['divide_type'] = 2;
                $data['remarks'] = "供应商商品取消订单退款";
                if ($res->supplier_price > 0) {
                    $this->insert($data);
                }
                //将原来的商品账单改为已结算
                $this->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', $goods->supplier_work_id], ['divide_type', '=', 2], ['add_or_sub', '=', 1], ['settle_status', '=', 0]])
                    ->update(['settle_status' => 1, 'is_log' => 1]);
                //将工作室待结算金额剪掉
                (new AdminUser())->where([['id', '=', $goods->supplier_work_id]])->decrement("wait_surplus", $res->supplier_price);
            }
            //平台退回账单
            $info = $this
                ->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', 0], ['divide_type', '=', 3], ['add_or_sub', '=', 2]])
                ->first();
            if (empty($info)) {
                $data['money'] = $res->platform_money;
                $data['divide_type'] = 3;
                $data['work_id'] = 0;
                $data['remarks'] = "供应商商品取消订单退款";
                if ($res->platform_money > 0) {
                    $this->insert($data);
                }
                $this->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', 0], ['divide_type', '=', 3], ['add_or_sub', '=', 1], ['settle_status', '=', 0]])
                    ->update(['settle_status' => 1, 'is_log' => 1]);
            }
        }
        return true;
    }


    /***
     * @param $order_id
     * @return bool
     * 售后同意后直接退款
     */

    public function setAfterAgreeGoodsOrderBack($order_id)
    {
        $res = (new Order())->getOrderDetail($order_id);
        //退款成功
        $order_goods = $res->order_goods;
        $data['sn'] = $res->sn;
        $data['date_str'] = date("Y-m-d H:i:s", time());
        $data['goods_num'] = $res->goods_num;
        $data['names'] = $order_goods->goods_name;
        $data['work_id'] = $res->work_id;
        $data['goods_id'] = $order_goods->goods_id;
        $data['order_id'] = $res->id;
        $data['updated_at'] = date("Y-m-d H:i:s", time());
        $data['created_at'] = date("Y-m-d H:i:s", time());
        $data['type'] = 2;
        $data['settle_status'] = 1;
        $data['add_or_sub'] = 2;
        $goods = (new ProductGoods())->where([['id', '=', $order_goods->goods_id]])->first();
        $d_work = new DoctorWorks();
        $data['divide_type'] = 1;
        if ($goods->product_source == 1) { //工作室产品
            if ($goods->supplier_work_id == $res->work_id) { //工作室自己的产品
                $info = $this
                    ->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', $res->work_id], ['divide_type', '=', 1], ['add_or_sub', '=', 2]])
                    ->first();
                if (empty($info)) {
                    $data['money'] = $res->self_work_price;
                    $data['remarks'] = "售后订单商品退款";
                    if ($res->self_work_price > 0) {
                        $this->insert($data);
                    }
                    $d_work->where([['id', '=', $res->work_id]])->decrement("wait_surplus", $res->self_work_price);
                    $this->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', $res->work_id], ['divide_type', '=', 1], ['add_or_sub', '=', 1], ['settle_status', '=', 0]])
                        ->update(['settle_status' => 1, 'is_log' => 1]);
                }
            } else { //分享产品
                //本工作室账单情况
                $info = $this
                    ->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', $res->work_id], ['divide_type', '=', 1], ['add_or_sub', '=', 2]])
                    ->first();
                if (empty($info)) {
                    $data['remarks'] = "售后订单分享商品退款";
                    $data['money'] = $res->self_work_price;
                    if ($res->self_work_price > 0) {
                        $this->insert($data);
                    }
                    $d_work->where([['id', '=', $res->work_id]])->decrement("wait_surplus", $res->self_work_price);
                    $this->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', $res->work_id], ['divide_type', '=', 1], ['add_or_sub', '=', 1], ['settle_status', '=', 0]])
                        ->update(['settle_status' => 1, 'is_log' => 1]);
                }
                //分享工作室账单情况
                $info = $this
                    ->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', $goods->supplier_work_id], ['divide_type', '=', 1], ['add_or_sub', '=', 2]])
                    ->first();
                if (empty($info)) {
                    $data['remarks'] = "售后订单分享商品退款";
                    $data['money'] = $res->share_work_price;
                    $data['work_id'] = $goods->supplier_work_id;
                    if ($res->share_work_price > 0) {
                        $this->insert($data);
                    }
                    $d_work->where([['id', '=', $goods->supplier_work_id]])->decrement("wait_surplus", $res->share_work_price);
                    $this->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', $goods->supplier_work_id], ['divide_type', '=', 1], ['add_or_sub', '=', 1], ['settle_status', '=', 0]])
                        ->update(['settle_status' => 1, 'is_log' => 1]);
                }
            }
        } elseif ($goods->product_source == 2) {  //供应商产品
            //本工作室账单情况
            $info = $this
                ->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', $res->work_id], ['divide_type', '=', 1], ['add_or_sub', '=', 2]])
                ->first();
            if (empty($info)) {
                $data['money'] = $res->self_work_price;
                $data['remarks'] = "售后订单供应商商品退款";
                if ($res->self_work_price > 0) {
                    $this->insert($data);
                }
                $d_work->where([['id', '=', $res->work_id]])->decrement("wait_surplus", $res->self_work_price);
                $this->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', $res->work_id], ['divide_type', '=', 1], ['add_or_sub', '=', 1], ['settle_status', '=', 0]])
                    ->update(['settle_status' => 1, 'is_log' => 1]);
            }
            //供应商账单情况
            $su_info = $this
                ->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', $goods->supplier_work_id], ['divide_type', '=', 2], ['add_or_sub', '=', 2]])
                ->first();
            if (empty($su_info)) {
                $data['money'] = $res->supplier_price;
                $data['work_id'] = $goods->supplier_work_id;
                $data['divide_type'] = 2;
                $data['remarks'] = "售后订单供应商商品退款";
                if ($res->supplier_price > 0) {
                    $this->insert($data);
                }
                (new AdminUser())->where([['id', '=', $goods->supplier_work_id]])->decrement("wait_surplus", $res->supplier_price);
                $this->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['work_id', '=', $goods->supplier_work_id], ['divide_type', '=', 2], ['add_or_sub', '=', 1], ['settle_status', '=', 0]])
                    ->update(['settle_status' => 1, 'is_log' => 1]);
            }
        }
        (new Order())->where([['id', '=', $order_id]])->update(['end_settle' => 1]);
        return true;
    }


    /**
     * @param $order_id
     * @return bool
     * 商品订单确认收货后7天进账
     * 确认收货后7天进行入账
     */
    public function goodsOrderconfirmReceipt($order_id)
    {
        $work = new DoctorWorks();
        $goods = new ProductGoods();
        $admin_user = new AdminUser();
        $res = (new Order())->getOrderDetail($order_id);
        if (empty($res)) {
            return false;
        }
        $o_goods = $res->order_goods;
        $goods_info = $goods->where([['id', '=', $o_goods->goods_id]])->first();
        if (empty($goods_info)) {
            return false;
        }
        //修改账单状态(已结算)
        $this->where([['order_id', '=', $res->id], ['sn', '=', $res->sn], ['settle_status', '=', 0], ['add_or_sub', '=', 1]])->update(['settle_status' => 1]);
        if ($goods_info->product_source == 1) {  //自营
            if ($goods_info->supplier_work_id == $res->work_id) { //自己商品
                $work->where([["id", '=', $res->work_id]])->increment("surplus", $res->self_work_price);
                $work->where([["id", '=', $res->work_id]])->decrement("wait_surplus", $res->self_work_price);
            } else { //其他工作室产品  无用
                $work->where([["id", '=', $goods_info->supplier_work_id]])->increment("surplus", $res->share_work_price);
                $work->where([["id", '=', $goods_info->supplier_work_id]])->decrement("wait_surplus", $res->share_work_price);
                $work->where([["id", '=', $res->work_id]])->increment("surplus", $res->self_work_price);
                $work->where([["id", '=', $res->work_id]])->decrement("wait_surplus", $res->self_work_price);
            }
        } elseif ($goods_info->product_source == 2) { //供货商
            $admin_user->where([["id", '=', $goods_info->supplier_work_id]])->increment("surplus", $res->supplier_price);
            $admin_user->where([["id", '=', $goods_info->supplier_work_id]])->decrement("wait_surplus", $res->supplier_price);
            $work->where([["id", '=', $res->work_id]])->increment("surplus", $res->self_work_price);
            $work->where([["id", '=', $res->work_id]])->decrement("wait_surplus", $res->self_work_price);
        }
        (new Order())->where([['id', '=', $order_id]])->update(['end_settle' => 1]);
        return true;
    }


    /**
     *工作室折线图
     *
     */
    public function getEchartMapData($param)
    {
        $where = [];
        $where[] = ['work_id', '=', $param['work_id']];
        $where[] = ['add_or_sub', '=', 1];
        $w[] = ['work_id', '=', $param['work_id']];
        $w[] = ['add_or_sub', '=', 0];
        $result = [];
        for ($i = 0; $i <= 11; $i++) {
            $date = $this->setMonthStartEndTime($i);
            $res = $this->setDataMoney($where, $w, $date['start'], $date['end']);
            $result['leida'][] = ["name" => $date['year'] . "年" . $date['month'] . "月"];
            $result['time'][] = $date['year'] . "年" . $date['month'] . "月";
            $result['money'][] = $res['day_all_money'];

        }
        return $result;
    }

    /**
     * @param $num
     * @return array
     * 获取月份开始时间和结束时间
     */
    public function setMonthStartEndTime($num)
    {
        $now = Carbon::now();
        // 获取上两个月的时间（即当前月份的上一个月和上上个月）
        $lastTwoMonth = $now->subMonths($num);
        // 获取上两月的开始时间
        $startOfLastTwoMonth = $lastTwoMonth->startOfMonth();
        $start = $startOfLastTwoMonth->format('Y-m-d H:i:s');
        $month = $startOfLastTwoMonth->format('m');
        $year = $startOfLastTwoMonth->format('Y');
        // 获取上两月的结束时间
        $endOfLastTwoMonth = $lastTwoMonth->endOfMonth();
        $end = $endOfLastTwoMonth->format('Y-m-d H:i:s');
        return ['start' => $start, 'end' => $end, "month" => $month, "year" => $year];
    }


    /**
     * 医护人员取消接单或者到预约时间后取消订单
     * 退款后添加退款账单记录
     */
    public static function setBackConsultOrder($order, $order_goods)
    {
        //退款成功后操作
        $case = (new CaseDescribe())->where([['id', '=', $order_goods->specs_id]])->first();
        $data['sn'] = $order->sn;
        $data['date_str'] = date("Y-m-d H:i:s", time());
        $data['goods_num'] = $order->nums;
        $data['names'] = $order_goods->goods_name;
        $data['work_id'] = $order->work_id;
        $data['goods_id'] = $order_goods->goods_id;
        $data['order_id'] = $order->id;
        $data['updated_at'] = date("Y-m-d H:i:s", time());
        $data['created_at'] = date("Y-m-d H:i:s", time());
        $data['sex'] = isset($case->sex) ? $case->sex : 0;
        $data['age'] = isset($case->age) ? $case->age : 0;
        $data['type'] = 3;
        $data['settle_status'] = 1;
        $data['add_or_sub'] = 2;
        $data['divide_type'] = 1;
        $data['money'] = $order->self_work_price;
        if ($order->order_type == 3) {
            $ty = 1;
        } elseif ($order->order_type == 4) {
            $ty = 2;
        }
        $data['consult_type'] = $ty;
        $data['consult_duration'] = $order_goods->duration;
        $data['remarks'] = "咨询取消退款";
        $wk = new DoctorWorks();
        //工作室退还账单
        $info = self::where([['order_id', '=', $order->id], ['sn', '=', $order->sn], ['work_id', '=', $order->work_id], ['divide_type', '=', 1], ['add_or_sub', '=', 2]])
            ->first();
        if (empty($info)) {
            //余额减掉
//            (new DoctorWorks())->where([['id','=',$order->work_id]])->decrement("surplus",$order->self_work_price);
            if ($order->self_work_price > 0) {
                self::setStatementsLog($data);
            }
            //将原来未结算订单设置为结算并标记
            self::where([['order_id', '=', $order->id], ['sn', '=', $order->sn], ['work_id', '=', $order->work_id], ['divide_type', '=', 1], ['add_or_sub', '=', 1], ['settle_status', '=', 0]])
                ->update(['settle_status' => 1, 'is_log' => 1]);
            $wk->where([['id', '=', $order->work_id]])->decrement("wait_surplus", $order->self_work_price);
        }
        //平台退还账单
        $platform = self::where([['order_id', '=', $order->id], ['sn', '=', $order->sn], ['work_id', '=', 0], ['divide_type', '=', 3], ['add_or_sub', '=', 2]])
            ->first();
        if (empty($platform)) {
            $data['divide_type'] = 3;
            $data['work_id'] = 0;
            $data['money'] = $order->platform_money;
            if ($order->platform_money > 0) {
                self::setStatementsLog($data);
            }
            //将原来未结算订单设置为结算并标记
            self::where([['order_id', '=', $order->id], ['sn', '=', $order->sn], ['work_id', '=', 0], ['divide_type', '=', 3], ['add_or_sub', '=', 1], ['settle_status', '=', 0]])
                ->update(['settle_status' => 1, 'is_log' => 1]);
        }
        (new Doctor())->where([['id', '=', $order_goods->goods_id]])->decrement("consult_num", 1);
        (new Order)->where([['id', '=', $order->id]])->increment("end_settle", 1);
        return true;
    }


    /**
     * 完成咨询
     * 咨询确认收货
     * 账单修改,工作室加钱
     *
     */

    public static function setCompleteConsultOrder($order, $dl)
    {
        $work = new DoctorWorks();
        $dd = (new AddressBook())
            ->where([['user_id', '=', $order->user_id], ['work_id', '=', $order->work_id]])->first();
        if ($dd) {
            (new AddressBook())
                ->where([['user_id', '=', $order->user_id], ['work_id', '=', $order->work_id]])
                ->update(['last_chat_time' => date("Y-m-d H:i:s", time())]);
        }
        //如果是图文咨询0元购
        if($order->order_price == 0 && $order->order_type == 3){
            (new Order)->where([['id', '=', $order->id]])->increment("end_settle", 1);
            return true;
        }
        //分账
        $statement = self::where([['order_id', '=', $order->id], ['sn', '=', $order->sn], ['work_id', '=', $order->work_id], ['settle_status', '=', 0], ['add_or_sub', '=', 1], ['divide_type', '=', 1]])->first();
        if ($statement) {
            self::where([['order_id', '=', $order->id], ['sn', '=', $order->sn]])->update(['settle_status' => 1, 'consult_duration' => $dl['use_time']]);
            $work->where([['id', '=', $order->work_id]])->increment("surplus", $order->self_work_price); //增加结算
            $work->where([['id', '=', $order->work_id]])->decrement("wait_surplus", $order->self_work_price); //待结算金额减掉
            //设置结算状态
            (new Order)->where([['id', '=', $order->id]])->increment("end_settle", 1);
        }
        return true;
    }

    /**
     * @param $order_id
     * 拒绝打赏订单打赏记录/退回打赏
     */
    public function setRefuseGiveRewardOrderBack($order)
    {
        //判断打赏的是工作室还是医生
        if ($order->type == 10) {
            $doc_info = (new Doctor())->where([['id', '=', $order->store_id]])->first();
            $data['names'] = $doc_info->real_name;
        } elseif ($order->type == 20) {
            $work_info = (new DoctorWorks())->where([['id', '=', $order->store_id]])->first();
            $data['names'] = $work_info->work_name;
        }
        //打赏退款成功后操作
        $data['sn'] = $order->sn;
        $data['date_str'] = date("Y-m-d H:i:s", time());
        $data['goods_num'] = 1;
        $data['work_id'] = $order->work_id;
        $data['goods_id'] = $order->store_id;
        $data['order_id'] = $order->id;
        $data['updated_at'] = date("Y-m-d H:i:s", time());
        $data['created_at'] = date("Y-m-d H:i:s", time());
        $data['type'] = 4;
        $data['settle_status'] = 1;
        $data['add_or_sub'] = 2;
        $data['divide_type'] = 1;
        $data['money'] = $order->money;
        $data['remarks'] = "拒绝打赏退款";
        $data['is_log'] = 1;
        $wk = new DoctorWorks();
        //工作室退还账单
        $info = self::where([['order_id', '=', $order->id], ['sn', '=', $order->sn], ['work_id', '=', $order->work_id], ['divide_type', '=', 1], ['add_or_sub', '=', 2], ['type', '=', 4]])
            ->first();
        if (empty($info)) {
            //余额减掉
//            (new DoctorWorks())->where([['id','=',$order->work_id]])->decrement("surplus",$order->self_work_price);
            if ($order->money > 0) {
                self::setStatementsLog($data);
            }
            //将原来未结算订单设置为结算并标记
            self::where([['order_id', '=', $order->id], ['sn', '=', $order->sn], ['work_id', '=', $order->work_id], ['divide_type', '=', 1], ['add_or_sub', '=', 1], ['settle_status', '=', 0], ['type', '=', 4]])
                ->update(['settle_status' => 1, 'is_log' => 1]);
            $wk->where([['id', '=', $order->work_id]])->decrement("wait_surplus", $order->money);
        }
        //平台退还账单
//        $platform = self::where([['order_id', '=', $order->id], ['work_id', '=',0], ['divide_type', '=', 3], ['add_or_sub', '=', 2]])
//            ->first();
//        if (empty($platform)) {
//            $data['divide_type'] = 3;
//            $data['work_id'] = 0;
//            $data['money'] = $order->platform_money;
//            if($order->platform_money>0){
//                self::setStatementsLog($data);
//            }
//            //将原来未结算订单设置为结算并标记
//            self::where([['order_id', '=', $order->id], ['work_id', '=', 0], ['divide_type', '=', 3], ['add_or_sub', '=', 1], ['settle_status', '=', 0]])
//                ->update(['settle_status'=>1,'is_log'=>1]);
//        }
//        (new Doctor())->where([['id', '=', $order_goods->goods_id]])->decrement("consult_num", 1);
//        (new Order)->where([['id','=',$order->id]])->increment("end_settle",1);
        return true;
    }

    /***
     * @param $order
     * @return bool
     * 设置同意打赏账单
     */
    public function setAgreeGiveRewardOrder($order)
    {
        $work = new DoctorWorks();
        //分账
        $statement = self::where([['order_id', '=', $order->id], ['sn', '=', $order->sn], ['work_id', '=', $order->work_id], ['settle_status', '=', 0], ['add_or_sub', '=', 1], ['divide_type', '=', 1], ['type', '=', 4]])->first();
        if ($statement) {
            self::where([['sn', '=', $order->sn], ['type', '=', 4]])->update(['settle_status' => 1]);
            $work->where([['id', '=', $order->work_id]])->increment("surplus", $order->money); //增加结算
            $work->where([['id', '=', $order->work_id]])->decrement("wait_surplus", $order->money); //待结算金额减掉
            //设置结算状态
            (new StoreOrder())->where([['id', '=', $order->id]])->update(['order_status' => 1]);
        }
        return true;
    }


    /**
     * @param $order_id
     * 押金宝退回押金
     */
    public function setReturnBackUserMoneyOrderBack($order)
    {
        $order_goods = $order->order_goods;
        //判断押金宝订单
        if ($order->order_type == 5 && $order->order_status == 0) {
            //打赏退款成功后操作
            $data['sn'] = $order->sn;
            $data['date_str'] = date("Y-m-d H:i:s", time());
            $data['names'] = $order_goods->goods_name;
            $data['goods_num'] = $order_goods->goods_num;
            $data['work_id'] = $order->work_id;
            $data['goods_id'] = $order_goods->goods_id;
            $data['order_id'] = $order->id;
            $data['updated_at'] = date("Y-m-d H:i:s", time());
            $data['created_at'] = date("Y-m-d H:i:s", time());
            $data['type'] = 5;
            $data['settle_status'] = 1;
            $data['add_or_sub'] = 2;
            $data['divide_type'] = 1;
            if ($order_goods->is_deduct == 1) {
                $data['money'] = $order->self_work_price - $order_goods->deduct_money;
            } else {
                $data['money'] = $order->self_work_price;
                (new LeaseGoods())->where([['id', '=', $order_goods->goods_id]])->increment("stock_num", $order_goods->goods_num);
            }
            $data['remarks'] = "押金宝退回押金";
            $data['is_log'] = 1;
            $wk = new DoctorWorks();
            //工作室退还账单
            $info = self::where([['order_id', '=', $order->id], ['sn', '=', $order->sn], ['work_id', '=', $order->work_id], ['divide_type', '=', 1], ['add_or_sub', '=', 2], ['type', '=', 5]])
                ->first();
            if (empty($info)) {
                //商品库存加1
//            (new DoctorWorks())->where([['id','=',$order->work_id]])->decrement("surplus",$order->self_work_price);
                if ($order->self_work_price > 0) {
                    self::setStatementsLog($data);
                }
                //将原来未结算订单设置为结算并标记
                self::where([['order_id', '=', $order->id], ['sn', '=', $order->sn], ['work_id', '=', $order->work_id], ['divide_type', '=', 1], ['add_or_sub', '=', 1], ['settle_status', '=', 0], ['type', '=', 5]])
                    ->update(['settle_status' => 1, 'is_log' => 1]);
                $wk->where([['id', '=', $order->work_id]])->decrement("wait_surplus", $order->self_work_price);
                if ($order_goods->is_deduct == 1) { //扣除押金
                    $wk->where([['id', '=', $order->work_id]])->increment("surplus", $order_goods->deduct_money);
                    $wk->where([['id', '=', $order->work_id]])->increment("deposit_surplus", $order_goods->deduct_money); //押金宝可结算余额
                }
            }
        }
        return true;
    }

    /***
     * @param $order
     * 患者买断账单修改
     */
    public static function setPatientBuyCompleteBill($order)
    {
//        $order_goods = $order->order_goods;
        //判断押金宝订单
        if ($order->order_type == 5) {
            $wk = new DoctorWorks();
            //工作室退还账单
            $info = self::where([['order_id', '=', $order->id], ['sn', '=', $order->sn], ['work_id', '=', $order->work_id], ['divide_type', '=', 1], ['add_or_sub', '=', 1], ['type', '=', 5]])
                ->first();
            if (!empty($info)) {
                //商品库存加1
//                (new LeaseGoods())->where([['id', '=', $order_goods->goods_id]])->decrement("stock_num", $order_goods->goods_num);
                //将原来未结算订单设置为结算并标记
                self::where([['order_id', '=', $order->id], ['sn', '=', $order->sn], ['work_id', '=', $order->work_id], ['divide_type', '=', 1], ['add_or_sub', '=', 1], ['settle_status', '=', 0], ['type', '=', 5]])
                    ->update(['settle_status' => 1, 'is_log' => 1]);
                $wk->where([['id', '=', $order->work_id]])->decrement("wait_surplus", $order->self_work_price); //减掉待结算
                $wk->where([['id', '=', $order->work_id]])->increment("surplus", $order->self_work_price); //增加已结算
                $wk->where([['id', '=', $order->work_id]])->increment("deposit_surplus", $order->self_work_price); //押金宝可结算余额
            }
        }
        return true;
    }

    /**
     * @param $order
     * 查看是否是第一个课程订单,完成任务后加能力值
     */
    public static function setCourseFirst($order)
    {
        if ($order->order_type != 1) {
            return false;
        }
        //如果该工作室第一笔图文订单,加上成长值
        $nm = Order::where([['work_id', '=', $order->work_id], ['order_type', '=', 1], ['order_status', '=', 1], ['receive_status', '=', 1]])->count();
        if ($nm == 1) { //第一笔订单
            $lt['work_id'] = $order->work_id;
            TaskComplete::add($lt, 6);
            GrowthNumberLog::add($lt, 6);
        }
        return true;
    }


}
